package com.anolesoft.amljmes.fortysix.controller;

import com.anolesoft.amljmes.fortysix.service.info.MesBzzFollowService;
import com.anolesoft.amljmes.fortysix.dto.MesBzzFollowDto;
import com.anolesoft.yr.demo.BaseQueryParamVO;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import lombok.*;
import lombok.experimental.SuperBuilder;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.anolesoft.common.core.constant.HttpStatus;
import com.anolesoft.common.core.domain.LoginUser;
import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.common.security.util.SecurityUtils;
import com.anolesoft.yr.demo.PageResult;
import com.anolesoft.yr.util.Constants;
import org.springframework.http.MediaType;


import java.io.Serializable;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("mesbzzfollow")
public class MesBzzFollowController {
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @SuperBuilder
    @ToString(callSuper = true)
    @EqualsAndHashCode(callSuper=true)
    public static class DemoVO extends BaseQueryParamVO implements Serializable {
        private String peitaodanhao; // 配套单号
        private String hegebiaoqian; // 合格标签编号
        private Integer type;//  类型 0：绑定 1：解除
        private String factoryId;//厂代号
    }

    @Autowired
    MesBzzFollowService mesBzzFollowService;

    //查询
    @PostMapping(value="/query",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult queryMesBzzFollowInfo(MesBzzFollowController.DemoVO demoVO){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        demoVO.setFactoryId(loginUser.getFacCode());

        List<MesBzzFollowDto> allList = mesBzzFollowService.queryMesBzzFollowInfo(demoVO);
        System.out.println(">>>>>>"+allList.toString());
        int pn =  demoVO.getPageNo().intValue();
        int ps = demoVO.getPageSize().intValue();
        int endIndex = pn * ps > allList.size() ? allList.size() : pn*ps;
        List<MesBzzFollowDto> list = allList.subList( (pn-1) *ps, endIndex  );
        PageResult pageResult = new PageResult(list, demoVO.getPageSize(), demoVO.getPageNo(), allList.size());
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,pageResult);
    }

    //新增
    @PostMapping(value="/add", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult addMesBzzFollow(@RequestBody MesBzzFollowDto inFoDtoParam){
        System.out.println(">>>>>>add"+inFoDtoParam);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        inFoDtoParam.setCaozuoren(loginUser.getEmpUid());
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,mesBzzFollowService.addMesBzzFollow(inFoDtoParam));
    }

}
